package com.coin.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coin.domain.WorkIssue;
import com.coin.model.R;
import com.coin.service.WorkIssueService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Auther: 李 力
 * @Date: 2025/6/6
 * @Description: com.coin.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/workIssues")
@Api(tags = "客户工单的控制器")
public class WorkIssueController {

    @Autowired
    private WorkIssueService workIssueService;

    /**
     * @Description: 分页条件查询工单
     * @Author: 李 力
     * @Date: 2025/6/6
     * @Param: Page<WorkIssue> page, Integer status, String startTime, String endTime
     * @return: R
     */
    @GetMapping
    @ApiOperation(value = "分页条件查询工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "工单当前的处理状态"),
            @ApiImplicitParam(name = "startTime", value = "工单创建的起始时间"),
            @ApiImplicitParam(name = "endTime", value = "工单创建的截至时间"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),
    })
    @PreAuthorize("hasAuthority('work_issue_query')")
    public R<Page<WorkIssue>> findByPage(@ApiIgnore Page<WorkIssue> page, Integer status, String startTime, String endTime) {
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<WorkIssue> workIssuePage = workIssueService.findByPage(page, status, startTime, endTime);
        return R.ok(workIssuePage);
    }


    /**
     * @Description: 回复某个工单
     * @Author: 李 力
     * @Date: 2025/6/6
     * @Param: id answer
     * @return: R
     */
    @PutMapping
    @ApiOperation(value = "回复某个工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工单的ID"),
            @ApiImplicitParam(name = "answer", value = "工单的answer"),
    })
    @PreAuthorize("hasAuthority('work_issue_update')")
    public R work_issue_update(Long id, String answer) {
        boolean result = workIssueService.answer(id, answer);
        return result ? R.ok() : R.fail("回复失败");
    }

    @GetMapping("/workIssue")
    public R<Page<WorkIssue>> viewWorkIssue(Page<WorkIssue> page) {
        page.addOrder(OrderItem.desc("last_update_time"));
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        Page<WorkIssue> issuePage = workIssueService.viewWorkIssue(page, Long.valueOf(userId));
        return R.ok(issuePage);
    }

    /**
     * @Description: 会员添加工单
     * @Author: 李 力
     * @Date: 2025/6/21
     * @Param:
     * @return: R
     */
    @PostMapping("/addWorkIssue")
    public R addWorkIssue(@RequestBody WorkIssue workIssue) {
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        workIssue.setUserId(Long.valueOf(userId));
        workIssue.setStatus(1);
        boolean isOk = workIssueService.addWorkIssue(workIssue);
        return isOk ? R.ok() : R.fail();
    }
}
